﻿using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System;
using FileSystemControls.ListViewItems;
using FileSystemControls.Support;

namespace FileSystemControls.SubForms
{

    public partial class FormFileProperties : Form
    {

        FileListViewItem _currentItem;

        public FormFileProperties(FileListViewItem argCurrentItem)
        {
            //IntPtr iconHandle; //the handle to the system image list
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();
            Icon LargeIcon;

            Bitmap FileIcon;
            InitializeComponent();

            _currentItem = argCurrentItem;

            //iconHandle = NativeMethods.SHGetFileInfo(_currentItem.FullPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_LARGEICON | NativeMethods.SHGFI_TYPENAME | NativeMethods.SHGFI_TYPENAME);
            NativeMethods.SHGetFileInfo(_currentItem.FullPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_LARGEICON | NativeMethods.SHGFI_TYPENAME | NativeMethods.SHGFI_TYPENAME);
            LargeIcon = Icon.FromHandle(shinfo.hIcon);

            FileIcon = LargeIcon.ToBitmap();

            pictureBoxIcon.Height = FileIcon.Height;
            pictureBoxIcon.Width = FileIcon.Width;
            pictureBoxIcon.BackgroundImage = FileIcon;

            labelTypeOfFile.Text = _currentItem.FileType;
            labelOpensWith.Text = _currentItem.AssociatedExecutable;
            labelLocation.Text = _currentItem.Path;
            labelSize.Text = _currentItem.Length.ToString();
            labelCreatedDate.Text = _currentItem.CreateDate.ToLongDateString() + _currentItem.CreateDate.ToLongTimeString();
            labelModifiedDate.Text = _currentItem.ModifyDate.ToLongDateString() + _currentItem.ModifyDate.ToLongTimeString();

            textBoxFileName.Text = _currentItem.Name;
        }

        private void FormFileProperties_Load(object argSender, EventArgs argEventArgs)
        {
            //FileAttributes.ReadOnly &&
            checkBoxReadOnly.Checked = _currentItem.IsReadOnly;
            //FileAttributes.Hidden &&
            checkBoxHidden.Checked = _currentItem.IsHidden;
            //FileAttributes.Archive &&
            checkBoxArchive.Checked = _currentItem.IsArchive;
            //FileAttributes.System &&
            checkBoxSystem.Checked = _currentItem.IsSystem;
            //FileAttributes.Compressed &&
            checkBoxCompressed.Checked = _currentItem.IsCompressed;
            //FileAttributes.Encrypted &&
            checkBoxEncrypted.Checked = _currentItem.IsEncrypted;

            //FileAttributes.Device
            checkBoxDevice.Checked = _currentItem.IsDevice;
            //FileAttributes.Directory
            //TODO: 016 - Check where this is being opened from and decide if this form is just for files or for directories too
            checkBoxDirectory.Checked = !(_currentItem is FileListViewItem);
        }

        private void buttonApply_Click(object argSender, EventArgs argEventArgs)
        {
            _currentItem.IsReadOnly = false;

            //FileAttributes.Hidden &&
            _currentItem.IsHidden = checkBoxHidden.Checked;
            //FileAttributes.Archive &&
            _currentItem.IsArchive = checkBoxArchive.Checked;
            //FileAttributes.System &&
            _currentItem.IsSystem = checkBoxSystem.Checked;
            //FileAttributes.Compressed &&
            _currentItem.IsCompressed = checkBoxCompressed.Checked;
            //FileAttributes.Encrypted &&
            _currentItem.IsEncrypted = checkBoxEncrypted.Checked;

            _currentItem.Rename(textBoxFileName.Text, true);

            //FileAttributes.ReadOnly &&
            _currentItem.IsReadOnly = checkBoxReadOnly.Checked;
            buttonApply.Enabled = false;
        }

        private void checkBox_CheckedChanged(object argSender, EventArgs argEventArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonOK_Click(object argSender, EventArgs argEventArgs)
        {
            buttonApply_Click(argSender, argEventArgs);
            Close();
        }

        //TODO: 026 - Review these comments and implement them where necessary
        //FileAttributes.ReadOnly: SetAttributes() works
        //FileAttributes.Hidden: SetAttributes() works
        //FileAttributes.Archive: SetAttributes() works
        //FileAttributes.System: SetAttributes() works
        //FileAttributes.Compressed: WIN_API works
        //FileAttributes.Encrypted: File.Encrypt - Works but you have to trap for expired Recovery Certificates. (Pop-up error text message)

        //FileAttributes.Device: not changeable
        //FileAttributes.Directory: Not changeable

        //FileAttributes.Normal // Not a real attribute. Used to clear the other attributes.


        //To remove an attribute without affecting the others
        //   File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);


        //How to set ActiveDirectory Security.
        //http://support.microsoft.com/kb/899553
        //public void SetPermissions(String vPath, String UserName)
        //{
        //    ADsSecurity objADsSec;
        //    SecurityDescriptor objSecDes;
        //    AccessControlList objDAcl;
        //    AccessControlEntry objAce1;
        //    AccessControlEntry objAce2;
        //    Object objSIdHex;
        //    ADsSID objSId;

        //    objADsSec = new ADsSecurityClass();
        //    objSecDes = (SecurityDescriptor)(objADsSec.GetSecurityDescriptor("FILE://" + vPath));
        //    objDAcl = (AccessControlList)objSecDes.DiscretionaryAcl;

        //    objSId = new ADsSIDClass();
        //    objSId.SetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SAM, UserName.ToString());
        //    objSIdHex = objSId.GetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SDDL);

        //    // Add a new access control entry (ACE) object (objAce) so that the user has Full Control permissions on NTFS file system files.
        //    objAce1 = new AccessControlEntryClass();
        //    objAce1.Trustee = (objSIdHex).ToString();
        //    objAce1.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL;
        //    objAce1.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
        //    objAce1.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ONLY_ACE | 1;
        //    objDAcl.AddAce(objAce1);

        //    // Add a new access control entry object (objAce) so that the user has Full Control permissions on NTFS file system folders.
        //    objAce2 = new AccessControlEntryClass();
        //    objAce2.Trustee = (objSIdHex).ToString();
        //    objAce2.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_ALL;
        //    objAce2.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
        //    objAce2.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_INHERIT_ACE | 1;
        //    objDAcl.AddAce(objAce2);

        //    objSecDes.DiscretionaryAcl = objDAcl;

        //    // Set permissions on the NTFS file system folder.
        //    objADsSec.SetSecurityDescriptor(objSecDes, "FILE://" + vPath);

    }

    //private void button1_Click(object sender, System.EventArgs argEventArgs)
    //{
    //    try
    //    {
    //        // Set <Domain> to your domain name.
    //        // Set <UserName> to the user account.
    //        SetPermissions("C:\\Test", "<Domain>\\<UserName>");
    //        MessageBox.Show("Full Access control granted.");
    //    }
    //    catch (Exception ex)
    //    {
    //        MessageBox.Show(ex.Message);
    //    }
    //}
    //}
}
